home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / B-C / C++ FAQ Reference 1.0 / C++ FAQ Reference 1.0.rsrc / TEXT_1031.txt < prev    next >
Encoding:
Text File  |  1993-06-30  |  1.7 KB  |  11 lines

  1. In addition to being an abstraction mechanism that makes is-a-kind-of relationships explicit, inheritance can also be used as a means of 'incremental programming'.  A derived class inherits all the representation (bits) of its base class, plus all the base class' mechanism (code).  Another device (virtual functions, described below) allows derived classes to selectively override some or all of the base class' mechanism (replace and/or enhance the various algorithms).
  2.  
  3. This simple ability is surprisingly powerful: it effectively adds a 'third dimension' to programming.  After becoming fluent in C++, most programmers find languages like C and Ada to be 'flat' (a cute little book, 'Flatland', aptly describes those living in a two dimensional plane, and their disbelief about a strange third dimension that is somehow neither North, South, East nor West, but is 'Up').
  4.  
  5. As a trivial example, suppose you have a Linked List that is too slow, and you wish to cache its length.  You could 'open up' the List 'class' (or 'module'), and modify it directly (which would certainly be appropriate for such a simple situation), but suppose the List's physical size is critical, and some important client cannot afford to add the extra machine word to every List. Another option would be to textually copy the List module and modify the copy, but this increases the amount of code that must be maintained, and also presumes you have access to the internal source code of the List module.  The OO solution is to realize that a List that caches its length is-a-kind-of-a List, so we inherit:
  6.  
  7.     class FastList : public List {
  8.       int length;    //cache the length here
  9.     public:
  10.       //override operations so the cache stays 'hot'
  11.     };